package by.dvrudenko.avtobaza.webapp.page.createDriver;

import java.util.Date;

import javax.inject.Inject;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.Driver;
import by.dvrudenko.avtobaza.datamodel.User;
import by.dvrudenko.avtobaza.datamodel.enums.UserRole;
import by.dvrudenko.avtobaza.services.DriverService;
import by.dvrudenko.avtobaza.services.UserService;
import by.dvrudenko.avtobaza.webapp.page.BaseLayout;
import by.dvrudenko.avtobaza.webapp.page.home.HomePage;
import by.dvrudenko.avtobaza.webapp.panel.uploadFile.UploadFile;
import by.dvrudenko.avtobaza.webapp.utils.datepicker.JQueryDateField;

@AuthorizeInstantiation({ "ADMIN" })
public class CreateDriverPage extends BaseLayout {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CreateDriverPage.class);	

	@Inject
	private DriverService driverService;
	
	@Inject
	private UserService userService;
	
	private final Date date = new Date();
	
	public CreateDriverPage(final Driver driver, final User user) {
		super();

		// форма для создания водителя
		Form<Driver> formCreateDriver = new Form<Driver>("form", new CompoundPropertyModel<Driver>(driver));
		
		final TextField<String> driverName = new TextField<String>("name", new PropertyModel<String>(user, "name"));
		driverName.setLabel(new ResourceModel("p.home.createedit.user.name"));	
		driverName.add(new PropertyValidator<String>());
		formCreateDriver.add(driverName);
		
		final TextField<String> driverLastName = new TextField<String>("lastName");
		driverLastName.setLabel(new ResourceModel("p.home.createedit.driver.lastName"));		
		driverLastName.add(new PropertyValidator<String>());
		formCreateDriver.add(driverLastName);
		
		final TextField<String> driverLogin = new TextField<String>("login", new PropertyModel<String>(user, "login"));
		driverLogin.setLabel(new ResourceModel("p.home.createedit.user.login"));
		driverLogin.add(new PropertyValidator<String>());
		formCreateDriver.add(driverLogin);
				
		final TextField<String> driverPassword = new TextField<String>("password", new PropertyModel<String>(user, "password"));
		driverPassword.setLabel(new ResourceModel("p.home.createedit.user.password"));
		driverPassword.add(new PropertyValidator<String>());
		formCreateDriver.add(driverPassword);
		
		final TextField<String> driverEmail = new TextField<String>("email", new PropertyModel<String>(user, "email"));
		driverEmail.setLabel(new ResourceModel("p.home.createedit.user.email"));
		driverEmail.add(EmailAddressValidator.getInstance());
		driverEmail.add(new PropertyValidator<String>());
		formCreateDriver.add(driverEmail);
		
		final TextField<Date> driverBirthday = new JQueryDateField("birthday", new PropertyModel<Date>(this, "date"));
		formCreateDriver.add(driverBirthday);
		
		final TextField<Date> driverExperience = new JQueryDateField("experience", new PropertyModel<Date>(this, "date"));
		formCreateDriver.add(driverExperience);
				
		// загрузка фотографии водителя		
		final UploadFile uF = new UploadFile("driverFoto");
		formCreateDriver.add(uF);
		
		formCreateDriver.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				/* 
				 * при сабмите формы создается профиль User и Driver 				 
				 */
				driver.setAge(convertDateToTimestamp(driverBirthday));
				driver.setExperience(convertDateToTimestamp(driverExperience));
				driver.setWork(false);
				driver.setAcomplishRoutes(0);
				driver.setFoto(uF.path);
				user.setRole(UserRole.DRIVER);
				driver.setUser(user);
				userService.saveOrUpdate(user);
				driverService.saveOrUpdate(driver);
				LOGGER.info(CreateDriverPage.this.user.getLogin() + " create new driver " +driver.getId());
				setResponsePage(HomePage.class);
			}
		});
		
		add(formCreateDriver);
	}



}
